package handlers

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"sanrenx.com/xyxf/app/services"
	"sanrenx.com/xyxf/pkg/app"
	"sanrenx.com/xyxf/pkg/e"
	"sanrenx.com/xyxf/pkg/utils"
)

// @Tags 藏经
// @Summary 藏经列表
// @Accept application/json
// @Produce  json
// @Param page query integer true "页码"
// @Param category query integer false "类型"
// @Param order query string false "排序 hot or new"
// @Success 200 {object} app.Response
// @Router  /app/v2/sutra [get]
func SutraGetPageV2(c *gin.Context) {

	var (
		dataSrv services.SutraSrv
		params  struct {
			PageNum  int    `form:"page" binding:"required,number"`
			Category int    `form:"category"`
			OrderBy  string `form:"order"`
		}
	)

	appG := app.Gin{C: c}
	//校验绑定参数
	if err := c.ShouldBind(&params); err != nil {
		app.MarkErrors(err)
		appG.Response(http.StatusBadRequest, e.InvalidParams, nil)
		return
	}

	pageNum := params.PageNum
	offset, size := utils.GetPage(pageNum)
	if res, err := dataSrv.GetPageSutraByCategory(int64(offset), int64(size), []int{params.Category}, false, params.OrderBy); err == nil {
		//返回信息
		appG.Response(http.StatusOK, e.SUCCESS, map[string]interface{}{
			"page": pageNum,  //页码
			"size": len(res), //数量
			"list": res,      //列表
		})
		return
	}

	appG.Response(http.StatusInternalServerError, e.ErrorGetSutrasFail, nil)
}

// @Tags 藏经
// @Summary 藏经章回列表
// @Accept application/json
// @Produce  json
// @Param page query integer true "页码"
// @Param id query integer true "藏经ID"
// @Success 200 {object} app.Response
// @Router  /app/v2/sutra/chapter [get]
func SutraGetChapters(c *gin.Context) {
	var (
		dataSrv services.SutraSrv
		params  struct {
			PageNum int    `form:"page" binding:"required,number"`
			SutraID uint64 `form:"id" binding:"required,number"`
		}
	)
	appG := app.Gin{C: c}
	//校验绑定参数
	if err := c.ShouldBind(&params); err != nil {
		app.MarkErrors(err)
		appG.Response(http.StatusBadRequest, e.InvalidParams, nil)
		return
	}

	pageNum := params.PageNum
	offset, size := utils.GetPage(pageNum)
	if res, err := dataSrv.GetPageChapterBySutra(offset, size, params.SutraID); err == nil {
		//返回信息
		appG.Response(http.StatusOK, e.SUCCESS, map[string]interface{}{
			"page": pageNum,  //页码
			"size": len(res), //数量
			"list": res,      //列表
		})
		return
	}

	appG.Response(http.StatusInternalServerError, e.ErrorGetSutraChaptersFail, nil)
}

// @Tags 藏经
// @Summary 章回详情
// @Accept application/json
// @Produce json
// @Param id query integer true "文章ID"
// @Success 200 {object} app.Response
// @Router  /app/v2/sutra/chapter/details [get]
func SutraGetChapterDetailsV2(c *gin.Context) {

	var (
		sutraSrv services.SutraSrv
		params   struct {
			ChapterID uint64 `form:"id" binding:"required,number"`
		}
	)

	appG := app.Gin{C: c}
	//校验绑定参数
	if err := c.ShouldBind(&params); err != nil {
		app.MarkErrors(err)
		appG.Response(http.StatusBadRequest, e.InvalidParams, nil)
		return
	}

	//获取文章详情
	res, err := sutraSrv.GetChapterById(params.ChapterID)
	if err != nil {
		appG.Response(http.StatusInternalServerError, e.ErrorGetSutraChapterFail, nil)
		return
	}

	//返回信息
	appG.Response(http.StatusOK, e.SUCCESS, res)
}
